package psn.zjk.netty.server;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

import javax.net.ssl.SSLContext;

/**
 * @author zhangjk
 * @CREATE_TIME 2020/12/2 16:03
 */
public class ChildChannelInitializer extends ChannelInitializer {
    @Override
    protected void initChannel(Channel arg0) throws Exception {
        // ssl
        arg0.pipeline().addFirst(new SslHandler(SSLContext.getDefault().createSSLEngine()));
        // 行分割器
        arg0.pipeline().addLast(new LineBasedFrameDecoder(1024));
        // 指定分隔符解码器
        arg0.pipeline().addLast(new DelimiterBasedFrameDecoder(11, Unpooled.copiedBuffer("".getBytes())));
        // byte转字符串
        arg0.pipeline().addLast(new StringDecoder());
//        arg0.pipeline().addLast(new ServerHandler());
        // 定长解码器
        arg0.pipeline().addLast(new FixedLengthFrameDecoder(11));
        // 帧长度、偏移、长度域的长度
        arg0.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024, 0, 4));
        arg0.pipeline().addLast(new DelimiterBasedFrameDecoder(11, Unpooled.copiedBuffer("".getBytes())));
        // 服务端解码器
        arg0.pipeline().addLast(new HttpServerCodec());
        // 消息聚合器，FullHttpRequest，FullHttpResponse
        arg0.pipeline().addLast(new HttpObjectAggregator(512 * 1024));
        // 内容压缩器
        arg0.pipeline().addLast(new HttpContentDecompressor());
        // 大数据块处理器：ChunkedFile，ChunkedNioFile，ChunkedStream，ChunkedNioStream
        arg0.pipeline().addLast(new ChunkedWriteHandler());

        // JDK序列化
        // CompatibleObjectEncoder
        // CompactObjectInputStream
        // CompactObjectOutputStream
        // ObjectEncoder
        // ObjectDecoder
        // ObjectEncoderOutputStream
        // ObjectDecoderInputStream

        // jboss序列化
        // CompatibleMarshallingEncoder
        // CompatibleMarshallingDecoder
        // MarshallingEncoder
        // MarshallingDecoder

        // protobuf序列化
        // ProtobufDecoder
        // ProtobufEncoder
        // ProtobufVarint32FrameDecoder
        // ProtobufVarint32LengthFieldPrepender
    }
}